'use strict';
/**
 * Created by FreezeSoul on 2015/9/25.
 */
define([
        'angular',
        'jquery',
        'app/modules/player/define'
    ],
    function (angular, $) {
        angular.module('ad.player')
            .directive('adPlayerSidebarAddChartDirective', ['$rootScope', '$state', '$timeout', 'Notification', function ($rootScope, $state, $timeout, Notification) {
                return {
                    templateUrl: 'app/modules/player/views/add-chart.tpl.html',
                    restrict: 'E',
                    replace: true,
                    scope: {
                        chart: '=',
                        layout: '='
                    },
                    link: function ($scope, $element, $attrs) {
                        if ($scope.layout && $scope.chart) {
                            var canEditable = $scope.layout.canEditable();
                            var $parent = $($element).parent();
                            if (canEditable) {
                                $scope.layout.setDragable($parent, {"chartId": $scope.chart.chart_id});
                            }
                            $parent.on('mousedown', function () {
                                $timeout(function () {
                                    $scope.$emit("ad.player.sidebar.hide");
                                    if(!canEditable){
                                        Notification.success('布局不支持拖放指标！');
                                    }
                                }, 100);
                            });
                            $scope.$on('$destroy', function () {
                                $parent.off('mousedown');
                            });
                        }
                    }
                }
            }]);

    });
